﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LaserTurret : Turret
{
    private LineRenderer laser_line;
    public float laser_damage;
    public GameObject laser_effect;
    private void Awake()
    {
        laser_line = GetComponent<LineRenderer>();
    }

    protected override void Update()
    {
        if (enemy_list.Count > 0)
        {
            if (enemy_list[0] != null)
            {
                if (laser_line.enabled == false)
                {
                    laser_line.enabled = true;
                    laser_effect.SetActive(true);
                }
                temp = enemy_list[0].transform.position;
                turret_head.LookAt(new Vector3(temp.x, transform.position.y, temp.z));
                timer += Time.deltaTime;
                Attack();
            }
            else
            {
                ClearNull();
                timer = fire_rate; //上一个敌人死后能立即发射子弹
            }
        }
        else
        {
            if (laser_line.enabled == true)
            {
                laser_line.enabled = false;
                laser_effect.SetActive(false);
            }
        }
    }
    protected override void Attack()
    {
        laser_effect.transform.LookAt(fire_pos);
        laser_effect.transform.position = enemy_list[0].transform.position;
        laser_line.SetPositions(new Vector3[] { fire_pos.position, temp});
        if (timer >= fire_rate)
        {
            timer = 0;
            enemy_list[0].GetComponent<Enemy>().GetHurt(laser_damage);
        }
    }
}
